﻿using AIVision.Inspect;
using AIVision.Model.Events;
using AIVision.Model.InspectModels;
using AIVision.Model.MachineModels;
using AIVision.Model.UIModels;
using AIVision.PLC;
using AIVision.Repository.Entitys;
using AIVision.Repository.Services;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AIVision.UI.ViewModels
{
    public class CameraViewModel: BindableBase
    {
        private readonly CheckTempleteAgent cameraStationManager;
        public DelegateCommand<string> BtnCommand { get; private set; }

        public ObservableCollection<RunMsgModel> RunMsgList { get; set; } = new ObservableCollection<RunMsgModel>();
        private InspectStatusModel inspectStatus;
        public InspectStatusModel InspectStatus
        {
            get { return inspectStatus; }
            set { inspectStatus = value;RaisePropertyChanged(); }
        }

        private string viewName;
        public string ViewName
        {
            get { return viewName; }
            set { viewName = value; }
        }

        private string hsmart1DefectName ="";
        public string Hsmart1DefectName
        {
            get { return hsmart1DefectName; }
            set { hsmart1DefectName = value; RaisePropertyChanged(); }
        }
        private string hsmart2DefectName = "";
        public string Hsmart2DefectName
        {
            get { return hsmart2DefectName;  }
            set { hsmart2DefectName = value; RaisePropertyChanged(); }
        }
        private string hsmart3DefectName = "";
        public string Hsmart3DefectName
        {
            get { return hsmart3DefectName;  }
            set { hsmart3DefectName = value; RaisePropertyChanged(); }
        }
        private string hsmart4DefectName = "";
        public string Hsmart4DefectName
        {
            get { return hsmart4DefectName;  }
            set { hsmart4DefectName = value; RaisePropertyChanged(); }
        }
        public CameraViewModel(InspectStatusModel inspectStatus, CheckTempleteAgent cameraStationManager, IEventAggregator eventAggregator)
        {
            this.inspectStatus = inspectStatus;
            this.cameraStationManager = cameraStationManager;
            ViewName = "视觉检测";
            BtnCommand = new DelegateCommand<string>(BtnMethod);
            eventAggregator.GetEvent<InspectRunMsgEvent>().Subscribe(SubscribeInspectRunMsg, ThreadOption.UIThread);
            eventAggregator.GetEvent<ShowImageEvent>().Subscribe(SubscribeDefectName);
            cameraStationManager.Init();


        }
        private void BtnMethod(string cmd)
        { 
            switch(cmd)
            {
                case "TrigerPhoto":
                    inspectStatus.ManualPhotoReady = true;
                    break;
            }
        }


        private void SubscribeInspectRunMsg(RunMsgModel msg)
        {
            RunMsgList.Add(msg);
            Trace.WriteLine(msg.Msg);
        }

        private void SubscribeDefectName(ShowImageModel arg)
        {
            if (arg.SourceImage == null)
                return;
            switch (arg.HSmartWindowName)
            {
                case "Hsmart1":
                    Hsmart1DefectName = "";
                    if (arg.Defects.Count > 0)
                    {
                        foreach (var defect in arg.Defects)
                        {
                            Hsmart1DefectName += defect.Key + ";";
                        }
                    }
                    break;
                case "Hsmart2":
                    Hsmart2DefectName = "";
                    if (arg.Defects.Count > 0)
                    {
                        foreach (var defect in arg.Defects)
                        {
                            Hsmart2DefectName += defect.Key + ";";
                        }
                    }
                    break;
                case "Hsmart3":
                    Hsmart3DefectName = "";
                    if (arg.Defects.Count > 0)
                    {
                        foreach (var defect in arg.Defects)
                        {
                            Hsmart3DefectName += defect.Key + ";";
                        }
                    }
                    break;
                case "Hsmart4":
                    Hsmart4DefectName = "";
                    if (arg.Defects.Count > 0)
                    {
                        foreach (var defect in arg.Defects)
                        {
                            Hsmart4DefectName += defect.Key + ";";
                        }
                    }
                    break;
            }
        }
    }
}
